﻿using nRoute.Utilities;
using System;
using System.Collections.Generic;

namespace nRoute.Mapping
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]
    public class ResourceMetaAttribute
        : ResourceMetaAttributeBase
    {
        private readonly string _name;
        private readonly Object _value;
        private readonly Type _targetResourceType;
        private readonly string _targetResourceKey;

        public ResourceMetaAttribute(string name, Object value)
            : this(name, value, null, null) { }

        public ResourceMetaAttribute(string name, Object value, Type targetResourceType)
            : this(name, value, targetResourceType, null) { }

        public ResourceMetaAttribute(string name, Object value, string targetResourceKey)
            : this(name, value, null, targetResourceKey) { }

        public ResourceMetaAttribute(string name, Object value, Type targetResourceType, string targetResourceKey)
        {
            Guard.ArgumentNotNullOrWhiteSpace(name, "name");

            _name = name;
            _value = value;
            _targetResourceType = targetResourceType;
            _targetResourceKey = targetResourceKey;
        }

#region Properties

        public string Name
        {
            get { return _name; }
        }

        public Object Value
        {
            get { return _value; }
        }

#endregion

#region Overrides

        protected internal override Type GetResourceType(Type targetType)
        {
            return _targetResourceType;
        }

        protected internal override void AppendMeta(Type resourceType, string key, IDictionary<string, object> metadata)
        {
            if (string.IsNullOrWhiteSpace(_targetResourceKey) || string.Equals(_targetResourceKey, key, Utilities.Compatability.INVARIANT_STRING_COMPARISION))
            {
                metadata.Add(_name, _value);
            }
        }

#endregion

    }
}
